<template>
	<view style="padding-bottom: 100upx;" v-if="XCXIsSelect != '否'">
		<view class="text-center text-red padding-top-sm" v-if="form.remek&&renzheng==3">拒绝原因：{{form.remek}}</view>
		<view class=" padding bg radius margin">
			<u-form :model="form" ref="uForm" label-position="top" :label-style='lableStyle'>
				<u-form-item label="企业名称" :border-bottom='false'>
					<u-input placeholder="请输入企业名称" v-model="form.companyName" maxlength="11" />
				</u-form-item>
				<view style="width: 100%;height:1rpx;background: #f4f5f6;position: relative;top: -25rpx;"></view>
				<u-form-item label="法人姓名" :border-bottom='false'>
					<u-input placeholder="请输入法人姓名" v-model="form.companyLegalPerson" />
				</u-form-item>
				<view style="width: 100%;height:1rpx;background: #f4f5f6;position: relative;top: -25rpx;"></view>
				<u-form-item label="联系方式" :border-bottom='false'>
					<u-input placeholder="请填写（必填）" v-model="form.companyPhone" type="number" maxlength="11" />
				</u-form-item>
				<view style="width: 100%;height:1rpx;background: #f4f5f6;position: relative;top: -25rpx;"></view>
				<u-form-item label="地址" :border-bottom='false'>
					<u-input placeholder="请选择当前位置" v-model="form.companyAddress" @click="getAddress()" />
				</u-form-item>
			</u-form>
		</view>
		<view class=" padding bg radius margin">
			<view class="text-lg margin-top-sm">上传营业执照</view>
			<view class="flex" style="overflow: hidden;flex-direction: initial;">
				<view class="margin-top flex margin-right-sm" v-if="form.companyImage.length">
					<view class="flex" style="width: 200rpx;height: 200rpx;margin-right: 10rpx;position: relative;">
						<image :src="form.companyImage" style="width: 100%;height: 100%;"></image>
						<view style="z-index: 9;position: absolute;top: -15rpx;right: -15rpx;" @click="frontRemove(1)">
							<u-icon name="close-circle-fill" color="#2FB57A" size="50rpx"></u-icon>
						</view>
					</view>
				</view>
				<view class="margin-top" @click="addImage(1)" v-if="form.companyImage.length<=0">
					<view style="width: 200rpx;height: 200rpx;background: #f4f5f6;"
						class="flex justify-center align-center">
						<view>
							<view class="text-center">
								<image src="../../static/images/my/add.png" style="width: 65rpx;height: 55rpx;">
								</image>
							</view>
							<view class="text-center">添加图片</view>
						</view>
					</view>
				</view>

			</view>
		
			<view class="text-lg margin-top-sm">logo上传</view>
			<view class="flex" style="overflow: hidden;flex-wrap: wrap;">
				<view class="margin-top flex margin-right-sm flex-wrap" v-if="form.companyLogo && form.companyLogo.length">
					<view class="flex" style="width: 200rpx;height: 200rpx;margin-right: 5rpx;position: relative;">
						<image :src="form.companyLogo" style="width: 100%;height: 100%;"></image>
						<view style="z-index: 9;position: absolute;top: -15rpx;right: -15rpx;"
							@click="headImgremove(index)">
							<u-icon name="close-circle-fill" color="#2FB57A" size="50rpx"></u-icon>
						</view>
					</view>
				</view>
				<view class="margin-top" @click="addImage(4)" v-if="form.companyLogo.length<=0">
					<view style="width: 200rpx;height: 200rpx;background: #f4f5f6;"
						class="flex justify-center align-center">
						<view>
							<view class="text-center">
								<image src="../../static/images/my/add.png" style="width: 65rpx;height: 55rpx;">
								</image>
							</view>
							<view class="text-center">添加图片</view>
						</view>
					</view>
				</view>
			</view>
			<view class="text-lg margin-top-sm">持有资质证书（可多张）</view>
			<view class="flex" style="overflow: hidden;flex-wrap: wrap;">
				<view class="margin-top flex margin-right-sm flex-wrap" v-if="companyCertifivation.length">
					<view class="flex" style="width: 200rpx;height: 200rpx;margin-right: 2rpx;position: relative;"
						v-for="(image,index) in companyCertifivation" :key="index">
						<image :src="image" style="width: 100%;height: 100%;"></image>
						<view style="z-index: 9;position: absolute;top: -15rpx;right: -15rpx;"
							@click="removeImg(index)">
							<u-icon name="close-circle-fill" color="#2FB57A" size="50rpx"></u-icon>
						</view>
					</view>
				</view>
				<view class="margin-top" @click="addImages(2)" v-if="companyCertifivation.length<6">
					<view style="width: 200rpx;height: 200rpx;background: #f4f5f6;"
						class="flex justify-center align-center">
						<view>
							<view class="text-center">
								<image src="../../static/images/my/add.png" style="width: 65rpx;height: 55rpx;">
								</image>
							</view>
							<view class="text-center">添加图片</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view style="text-align: center;">
			<view class="footer">
				<image v-if="showAgree" @tap="isShowAgree"
					src="https://anmo.xianmxkj.com/file/uploadPath/2023/02/15/ee4b62258955340385364b67c57c65e0.png"
					style="width: 30upx;height: 30upx;"></image>
				<image v-else @tap="isShowAgree"
					src="https://anmo.xianmxkj.com/file/uploadPath/2023/01/18/02d699f656ea68f69a67ff906c8f1af6.png"
					style="width: 30upx;height: 30upx;"></image>
				<text style="margin-left: 10upx;margin-right: 0;" @tap="isShowAgree">我阅读并同意</text>
				<!-- 协议地址 -->
				<navigator url="/my/setting/xieyi?id=391" open-type="navigate">《商家入驻协议》</navigator>
			</view>
		</view>
		<view class="tabbar" @click="submit()" v-if="renzheng == 0||renzheng==3">提交发布</view>

		<!-- 性别选择 -->
		<u-action-sheet :list="actionSheetList" v-model="show" @click="actionSheetCallback"></u-action-sheet>
		<!-- 出生日期 -->
		<u-picker v-model="showData" :params="params" @confirm="bindData"></u-picker>
		
	</view>
</template>

<script>
	import configdata from '../../common/config.js';
	export default {
		data() {
			return {
				showAgree: false,
				form: {
					companyName:'',
					companyLegalPerson:'',
					companyPhone:'',
					companyAddress:'',
					companyImage: '',
					//头像上传
					companyLogo: '',
					//资质证书
					companyCertifivation: [],
					remek:''
				},
				// disabled: false,
				lableStyle: {
					color: '#000000',
					fontSize: '36upx'
				},
				
				renzheng: 0,

				show: false,
				actionSheetList: [{
					text: '男',
					label: '1',
				}, {
					text: '女',
					label: '2',
				}],
				showData: false,
				params: {
					year: true,
					month: true,
					day: true,
					hour: false,
					minute: false,
					second: false
				},
			
				//头像上传
				companyLogo: [],
				//资质证书
				companyCertifivation: [],
				// typeId: '',
				// sex: 0,
				closeable: true,
				XCXIsSelect: '否',
				companyId:''
				
			}
		},
		onLoad(e) {
			this.XCXIsSelect = this.$queue.getData("XCXIsSelect");
			if (this.XCXIsSelect != '否') {
				uni.setNavigationBarTitle({
					title: '商家认证'
				})
			} else {
				uni.setNavigationBarTitle({
					title: '关于我们'
				})
			}
			if (e.companyId) {
				this.companyId = e.companyId;
			}
			this.getUserInfo()

			
		
		},
		onShow() {
			this.renzheng = uni.getStorageSync("renzheng")
			// alert(this.renzheng,'this.renzhengthis.renzheng')
			// this.Getmoney()

		},
		methods: {
			isShowAgree() {
				//是否选择协议
				this.showAgree = !this.showAgree;
			},
			getAddress() {
				let that = this
				uni.chooseLocation({
					success: function(res) {
						console.log('位置名称：' + res.name);
						console.log('详细地址：' + res.address);
						console.log('纬度：' + res.latitude);
						console.log('经度：' + res.longitude);
						that.form.companyAddress = res.address
						// that.form.latitude = res.latitude
						// that.form.longitude = res.longitude
					}
				});
			},
			//日期弹框
			birthData() {
				this.showData = true
			},
			//性别选择
			bindSex() {
				this.show = true
			},
			
			// 保证金
			Getmoney() {
				this.$Request.get("/app/common/type/239").then(res => {
					console.log(res)
					if (res.code == 0) {
						this.money = res.data.value
					}
				});
			},
			// 点击actionSheet回调 性别
			actionSheetCallback(index) {
				console.log(index)
				this.sex = this.actionSheetList[index].label;
				this.form.sex = this.actionSheetList[index].text;
				// this.from.sexs =  this.actionSheetList[index].label;
				console.log(this.sex)
			},
			//出生日期
			bindData(e) {
				console.log(e)
				this.form.birthdate = e.year + '年' + e.month + '月' + e.day + '日'
				this.form.birth = e.year + '-' + e.month + '-' + e.day

			},
			//身份证删除
			frontRemove(index) {
				if (index == 1) {
					this.form.front = ''
				} else if (index == 2) {
					this.forms.front = ''
				}
			},
			//资质证书删除
			removeImg(index) {
				this.companyCertifivation.splice(index, 1)
			},
			backRemove() {
				this.form.back = ''
			},
			infantImgremove(index) {
				this.infantImg.splice(index, 1)
			},
			headImgremove(index) {
				this.form.companyLogo = ''
			},
			submit() {

				this.form.companyCertifivation = this.companyCertifivation
				this.form.companyCertifivation = this.form.companyCertifivation.toString();

				if(!this.showAgree){
					uni.showToast({
						title: '请阅读并同意《商家入驻协议》',
						icon: 'none',
						duration: 1000
					})
					return;
				}
				
				if (!this.form.companyName) {
					uni.showToast({
						title: '请输入企业名称',
						icon: 'none',
						duration: 1000
					})
					return;
				}

				if (!this.form.companyLegalPerson) {
					uni.showToast({
						title: '请输入法人姓名',
						icon: 'none',
						duration: 1000
					})
					return;
				}
				if (!this.form.companyPhone) {
					uni.showToast({
						title: '请输入联系方式',
						icon: 'none',
						duration: 1000
					})
					return;
				}
				if (!this.form.companyAddress) {
					uni.showToast({
						title: '请选择地址',
						icon: 'none',
						duration: 1000
					})
					return;
				}
				
				if (!this.form.companyImage) {
					uni.showToast({
						title: '请上传营业执照',
						icon: 'none',
						duration: 1000
					})
					return;
				}
				
				if (!this.form.companyLogo) {
					uni.showToast({
						title: '请上传logo',
						icon: 'none',
						duration: 1000
					})
					return;
				}
				if (!this.form.companyCertifivation) {
					uni.showToast({
						title: '请上传资质证书',
						icon: 'none',
						duration: 1000
					})
					return;
				}
				let userId = uni.getStorageSync('userId')
				
				if(this.companyId){
					let data = {
						companyName: this.form.companyName,
						companyLegalPerson: this.form.companyLegalPerson,
						companyPhone: this.form.companyPhone,
						companyAddress: this.form.companyAddress,
						companyImage: this.form.companyImage,
						companyLogo: this.form.companyLogo,
						companyCertification: this.form.companyCertifivation,
						companyId:this.companyId
					}
					this.$Request.postJson("/app/company/updateCompany", data).then(res => {
						if (res.code == 0) {
							uni.showToast({
								title: '认证提交成功！',
								icon: 'none'
							})
							setTimeout(function() {
								uni.switchTab({
									url: '/pages/my/index'
								})
							}, 1000)
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
						}
					});
				}else{
					let data = {
						companyName: this.form.companyName,
						companyLegalPerson: this.form.companyLegalPerson,
						companyPhone: this.form.companyPhone,
						companyAddress: this.form.companyAddress,
						companyImage: this.form.companyImage,
						companyLogo: this.form.companyLogo,
						companyCertification: this.form.companyCertifivation,
						userId:userId
					}
					this.$Request.postJson("/app/company/insertCompany", data).then(res => {
						if (res.code == 0) {
							uni.showToast({
								title: '认证提交成功！',
								icon: 'none'
							})
							setTimeout(function() {
								uni.switchTab({
									url: '/pages/my/index'
								})
							}, 1000)
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
						}
					});
				}
				


			},

			getUserInfo() {
				this.$Request.get("/app/company/selectCompanyByUserId").then(res => {
					console.log(res)
					if (res.code == 0 && res.data) {
						this.form.companyName = res.data.companyName
						this.form.companyLegalPerson = res.data.companyLegalPerson
						this.form.companyPhone = res.data.companyPhone
						this.form.companyAddress = res.data.companyAddress
						this.form.companyImage= res.data.companyImage
						//头像上传
						this.form.companyLogo= res.data.companyLogo
						//资质证书
						if (res.data.companyCertification) {
							this.companyCertifivation = res.data.companyCertification.split(',')
							this.form.companyCertifivation = res.data.companyCertification
						}
						if (res.data.status == 1) {
							this.disabled = true
						}
						if (res.data.status ==3) {
							this.form.remek = res.data.auditContent ? res.data.auditContent : ''
						}
						
					}
				});
			},

			addImages(e) {
				let that = this
				let urlName = 'https://merchant.3000am.com/sqx_fast/alioss/upload';
				if (e == 2) {
					urlName = 'https://merchant.3000am.com/sqx_fast/alioss/uploadWatermark';
				}
				uni.chooseImage({
					count: 6,
					sourceType: ['album', 'camera'],
					success: res => {
						for (let i = 0; i < res.tempFilePaths.length; i++) {
							that.$queue.showLoading("上传中...");
							uni.uploadFile({ // 上传接口
								// url: this.config("APIHOST1") + '/alioss/upload', //真实的接口地址
								url: urlName,
								filePath: res.tempFilePaths[i],
								name: 'file',
								success: (uploadFileRes) => {
									 if (e == 2) {
										if (that.companyCertifivation.length <= 5) {
											that.companyCertifivation.push(JSON.parse(uploadFileRes.data)
												.data)
										}
									}

									uni.hideLoading();
								}
							});
						}
					}
				})
			},
			addImage(e) {
				let that = this
				uni.chooseImage({
					count: 1,
					sourceType: ['album', 'camera'],
					success: res => {
						for (let i = 0; i < res.tempFilePaths.length; i++) {
							that.$queue.showLoading("上传中...");
							uni.uploadFile({ // 上传接口
								// url: this.config("APIHOST1") + '/alioss/upload', //真实的接口地址
								url: 'https://merchant.3000am.com/sqx_fast/alioss/upload',
								filePath: res.tempFilePaths[i],
								name: 'file',
								success: (uploadFileRes) => {
									if (e == 1) {
										that.form.companyImage = JSON.parse(uploadFileRes.data).data
									} else if (e == 4) {
										that.form.companyLogo = JSON.parse(uploadFileRes.data).data
										console.log(that.form.companyLogo)
									}
									uni.hideLoading();

								}
							});
						}
					}
				})
			},
			config: function(name) {
				var info = null;
				if (name) {
					var name2 = name.split("."); //字符分割
					if (name2.length > 1) {
						info = configdata[name2[0]][name2[1]] || null;
					} else {
						info = configdata[name] || null;
					}
					if (info == null) {
						let web_config = cache.get("web_config");
						if (web_config) {
							if (name2.length > 1) {
								info = web_config[name2[0]][name2[1]] || null;
							} else {
								info = web_config[name] || null;
							}
						}
					}
				}
				return info;
			}
		}
	}
</script>

<style>
	page {
		background-color: #F7F7F7;
	}

	.bg {
		background-color: #FFFFFF;
	}

	.footer {
		padding-left: 30upx;
		font-size: 24upx;
		color: #666666;
		text-align: center;
		display: flex;
		margin-bottom: 20rpx;
	}

	.u-input__input {
		color: balck !important;
	}

	.title_btn {
		height: 78upx;
		line-height: 78upx;
		/* background: #f7f7f7; */
	}

	.tabbar {
		width: 690upx;
		height: 88upx;
		background: #2FB57A;
		border-radius: 8upx;
		margin: 0 auto;
		text-align: center;
		line-height: 88upx;
		color: #FFFFFF;
	}

	.popup_pay {

		width: 100%;
		position: relative;
		padding-bottom: 45rpx;
		/* height: 160px; */
		/* #ifndef MP-WEIXIN */
		/* height: 130px; */
		/* #endif */

	}

	.pay_btn {
		width: 90%;
		margin: 0 auto;
		text-align: center;
		background: #1777FF;
		height: 80rpx;
		border-radius: 16rpx;
		color: #ffffff;
		line-height: 80rpx;

	}

	.hb {
		/* #ifdef H5 */
		padding-bottom: 180rpx;
		/* #endif */
	}

	.bgCol2 {
		color: #557EFD;
	}
</style>
